<?php

namespace app\common\func;


use Firebase\JWT\JWT;
use think\Db;

class Func
{
    /**
     * GCJ-02(高德)和BD-09(百度)之间转换
     * @param string $from 原始格式 gcj-02|bd-09
     * @param string $to 目标格式 gcj-02|bd-09
     * @param array $point 原始坐标值
     * @return array  转换后的坐标值
     * @throws \Exception
     */

    public static function coordinateTranslate($from, $to, array $point)
    {
        $pi = pi() * 3000.0 / 180.0;
        $from = strtoupper($from);
        $to = strtoupper($to);
        list($x, $y) = $point;

        if ($from === 'GCJ-02' && $to === 'BD-09') {
            $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $pi);
            $theta = atan2($y, $x) + 0.000003 * cos($x * $pi);
            return [$z * cos($theta) + 0.0065, $z * sin($theta) + 0.006];
        } else if ($from === 'BD-09' && $to === 'GCJ-02') {
            $x = $x - 0.0065;
            $y = $y - 0.006;
            $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $pi);
            $theta = atan2($y, $x) - 0.000003 * cos($x * $pi);
            return [$z * cos($theta), $z * sin($theta)];
        }
        throw new \Exception("unsupport $from to $to translate");
    }

    public static function jwtGetToken($data, $isRefresh = false, $expire = 7200, $refreshExpire = 86400 * 30)
//    public static function jwtGetToken($data, $isRefresh = false, $expire = 10, $refreshExpire = 20)
    {
        $key = "hqpz_local_key";
        $token = array(
            'iss' => '',                //签发人(官方字段:非必需)
            'aud' => '',              //受众(官方字段:非必需)
            'iat' => time(),               //签发时间(官方字段:非必需)
            'nbf' => time(),               //生效时间(官方字段:非必需)
            'data' => $data,     //过期时间(官方字段:非必需)
//            'exp' => time() + intval($expire),     //过期时间(官方字段:非必需)
        );

        $access_token = $token;
//        $access_token['data'] = $data;     //自定义字段
        $access_token['scopes'] = 'role_access'; //token标识，请求接口的token
        $access_token['exp'] =  time() + intval($expire); //access_token过期时间,这里设置2个小时

        $refresh_token = $token;
        $refresh_token['scopes'] = 'role_refresh'; //token标识，刷新access_token
        $refresh_token['exp'] = time() + intval($refreshExpire); //access_token过期时间,这里设置30天

       if(!$isRefresh){
           $jsonList = [
               'access_token'=>JWT::encode($access_token ,$key),
               'refresh_token'=>JWT::encode($refresh_token, $key),
//            'token_type'=>'bearer' //token_type：表示令牌类型，该值大小写不敏感，这里用bearer
           ];
           return $jsonList;
       }else{
           return JWT::encode($access_token ,$key);
       }


//        var_dump($expire, $token);exit;
        /**
         * IMPORTANT:
         * You must specify supported algorithms for your application. See
         * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
         * for a list of spec-compliant algorithms.
         */
//        $jwt = JWT::encode($token, $key);
    }

    public static function jwtDecodeToken($jwt)
    {
        $key = "hqpz_local_key";
        $decoded = JWT::decode($jwt, $key, array('HS256'));
        return  $decoded;
    }


    public static function addScoreLocal($type, $openId)
    {
        //根据type获取积分
        $score = db('lite_score_rule')->where(['is_deleted' => 0])->find($type);
        $now = date("Y-m-d H:i:s");
        switch ($type){
            case 1:
                $score = $score['score'];
                break;
            default:
                break;
        }
        if(isset($score) && $score > 0){
            Db::startTrans();
            $res1 = Db::table('lite_member_score_record')->insert([
                'open_id' => $openId,
                'type' => 1,
                'score' => $score,
                'create_time' => $now,
                'score_rule_id' => $type,
                'is_deleted' => 0,
            ]);

            $res2 = Db::table('store_member')->where(['openid' => $openId])->setInc("score", $score);
            if($res1 && $res2){
                Db::commit();
            }
        }
        return true;
    }
}